﻿//参考@被遗忘的青色剑士 科技版MOD
//由@不勤奋的儒雅 提供部分注释解析
//作者:我不搞直播了
//已经在数据库注册的行为或组件要在前面标明//已注册;注册报错
//由销锋镝铸提供技术指导
using Engine;
using Game;
using GameEntitySystem;
using TemplatesDatabase;
using Engine.Graphics;
using Engine.Media;

namespace Mod//未注册
{
    public class ComponentCreatureBloodDisplay : GameEntitySystem.Component, IDrawable
    {
        private ComponentCreature m_componentCreature;
        private SubsystemModelsRenderer m_subsystemModelsRenderer;
        private SubsystemPlayers players;
        private static int[] m_drawOrders = new int[1];
        public int level;

        public int[] DrawOrders => m_drawOrders;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            this.m_componentCreature = this.Entity.FindComponent<ComponentCreature>(true);
            this.m_subsystemModelsRenderer = this.Project.FindSubsystem<SubsystemModelsRenderer>(true);
            this.players = this.Project.FindSubsystem<SubsystemPlayers>(true);
            this.level = valuesDictionary.GetValue<int>("level");
        }

        public void Draw(Camera camera, int drawOrder)
        {
            if (this.m_componentCreature == null)
                return;
            ComponentPlayer componentPlayer = this.players.ComponentPlayers.Count > 0 ? this.players.ComponentPlayers[0] : (ComponentPlayer)null;
            ComponentHerdBehavior component1 = this.m_componentCreature.ComponentBody.Entity.FindComponent<ComponentHerdBehavior>();
            ComponentHealth component2 = this.m_componentCreature.ComponentBody.Entity.FindComponent<ComponentHealth>();
            ComponentHerdBehavior component3 = componentPlayer.ComponentMiner.ComponentCreature.ComponentBody.Entity.FindComponent<ComponentHerdBehavior>();
            if (component1 != null && component3 != null && component1.HerdName == component3.HerdName)
                this.level = 0;
            else if ((double)component2.AttackResilience >= 240.0)
                this.level = 2;
            string text = this.m_componentCreature.DisplayName + "[血量:" + (this.m_componentCreature.ComponentHealth.Health * this.m_componentCreature.ComponentHealth.AttackResilience).ToString() + "]";
            Vector3 position = Vector3.Transform(this.m_componentCreature.ComponentBody.Position + 1.02f * Vector3.UnitY * this.m_componentCreature.ComponentBody.BoxSize.Y + new Vector3(0.0f, 0.3f, 0.0f), camera.ViewMatrix);
            Vector3 right = Vector3.TransformNormal(0.005f * Vector3.Normalize(Vector3.Cross(camera.ViewDirection, camera.ViewUp)), camera.ViewMatrix);
            Vector3 down = Vector3.TransformNormal(-0.005f * Vector3.UnitY, camera.ViewMatrix);
            if ((double)position.Z >= 0.0)
                return;
            Color color = Color.Lerp(this.level == 0 ? new Color(135, (int)byte.MaxValue, 99) : (this.level == 2 ? new Color(0, 145, (int)byte.MaxValue, 200) : Color.White), Color.Transparent, MathUtils.Saturate((float)(((double)position.Length() - 4.0) / 3.0)));
            if (color.A <= (byte)8)
                return;
            this.m_subsystemModelsRenderer.PrimitivesRenderer.FontBatch(ContentManager.Get<BitmapFont>("Fonts/Pericles"), 1, DepthStencilState.DepthRead, RasterizerState.CullNoneScissor, BlendState.AlphaBlend, SamplerState.LinearClamp).QueueText(text, position, right, down, color, TextAnchor.HorizontalCenter | TextAnchor.Bottom);
        }
    }
}
